package com.example.kalapp;

import java.util.ArrayList;
import java.util.Iterator;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, TextWatcher {
	
	private Button mBtn_gender;
	private String mGender = "M";
	private EditText mFld_age;
	private TextView mBtn_weight;
	private EditText mFld_weight;
	private TextView mBtn_height;
	private EditText mFld_height;
	private Button mBtn_activity;
	private double mActivity = 1.55;
	private EditText mFld_total_calories;
	private EditText mFld_chg_calories;
	private Button mBtn_calculate;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mBtn_gender = (Button) findViewById(R.id.btn_gender);
		mFld_age = (EditText) findViewById(R.id.fld_age);
		mBtn_weight = (TextView) findViewById(R.id.btn_weight);
		mFld_weight = (EditText) findViewById(R.id.fld_weight);
		mBtn_height = (TextView) findViewById(R.id.btn_height);
		mFld_height = (EditText) findViewById(R.id.fld_height);
		mBtn_activity = (Button) findViewById(R.id.btn_activity);
		
		mFld_total_calories = (EditText) findViewById(R.id.fld_total_calories);
		mBtn_calculate = (Button) findViewById(R.id.btn_calculate);
		// restore last session
		SharedPreferences sp = getSharedPreferences(getString(R.string.key_preferences), MODE_PRIVATE);
		String age = sp.getString(getString(R.string.key_age), "0");
		if (!age.equals("0")) {
			mFld_age.setText(age);
		}
		mGender = sp.getString(getString(R.string.key_gender), "M");
		if (!mGender.equals("M")) {
			String genders[] = getResources().getStringArray(R.array.kon_vaerdi);
			for (int i = 0, l = genders.length; i < l; i++) {
				if (mGender.equals(genders[i])) {
					mBtn_gender.setText(getResources().getStringArray(R.array.kon_indtast)[i]);
					break;
				}
			}
		}
		String weight = sp.getString(getString(R.string.key_weight), "0");
		if (!weight.equals("0")) {
			mFld_weight.setText(weight);
		}
		String height = sp.getString(getString(R.string.key_height), "0");
		if (!height.equals("0")) {
			mFld_height.setText(height);
		}
		String activity = sp.getString(getString(R.string.key_activity), "1.55");
		if (!activity.equals("1.55")) {
			mActivity = Double.parseDouble(activity);
			String activities[] = getResources().getStringArray(R.array.aktivitet_vaerdi);
			for (int i = 0, l = activities.length; i < l; i++) {
				if (activity.equals(activities[i])) {
					mBtn_activity.setText(getResources().getStringArray(R.array.aktivitet)[i]);
					break;
				}
			}
		}
		
		recalculate();
		mFld_age.addTextChangedListener(this);
		mBtn_gender.setOnClickListener(this);
		mBtn_weight.setOnClickListener(this);
		mFld_weight.addTextChangedListener(this);
		mBtn_height.setOnClickListener(this);
		mFld_height.addTextChangedListener(this);
		mBtn_activity.setOnClickListener(this);
		mBtn_calculate.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	public void onClick(View v) {
		if (v == mBtn_gender) {
			(new AlertDialog.Builder(this))
			.setItems(R.array.kon_indtast, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mBtn_gender.setText(getResources().getStringArray(R.array.kon_indtast)[which]);
					mGender = getResources().getStringArray(R.array.kon_vaerdi)[which];
					recalculate();
				}
			})
			.show();
		} else if (v == mBtn_activity) {
			(new AlertDialog.Builder(this))
			.setItems(R.array.Aktivitet_indtast, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					mBtn_activity.setText(getResources().getStringArray(R.array.Aktivitet_indtast)[which]);
					mActivity = Double.parseDouble(getResources().getStringArray(R.array.aktivitets_vaerdi)[which]);
					recalculate();
				}
			})
			.show();
		} else if (v == mBtn_calculate) {
			recalculate();
		}
	}

	private double getFieldValue(EditText fld) {
		double value = 0;
		String value_str = fld.getText().toString();
		if ((value_str != null) && (value_str.length() > 0)) {
			value = Double.parseDouble(value_str);
		}
		return value;
	}

	private void recalculate() {
		long bmr = 0;
		double age = getFieldValue(mFld_age);
		double weight = getFieldValue(mFld_weight);
		double height = getFieldValue(mFld_height);
		
		if (mGender.equals("M")) {
			bmr = Math.round(66 + (13.7 * weight) + (5 * height) - (6.8 * age));
		} 
		else {
			bmr = Math.round(655 + (9.6 * weight) + (1.8 * height) - (4.7 * age));
		}
		mFld_total_calories.setText(Long.toString(bmr));
		
	}

	public void afterTextChanged(Editable arg0) {
		recalculate();
	}

	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
	
	
	@Override
	protected void onPause() {
		SharedPreferences sp = getSharedPreferences(getString(R.string.key_preferences), MODE_PRIVATE);
		SharedPreferences.Editor spe = sp.edit();
		spe.putString(getString(R.string.key_age), Double.toString(getFieldValue(mFld_age)));
		spe.putString(getString(R.string.key_gender), mGender);
		spe.putString(getString(R.string.key_weight), Double.toString(getFieldValue(mFld_weight)));
		spe.putString(getString(R.string.key_height), Double.toString(getFieldValue(mFld_height)));
		spe.putString(getString(R.string.key_activity), Double.toString(mActivity));
		spe.commit();
		super.onPause();
	}
}